<%@ Assembly Name="Behemoth.SharePoint.Administration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" Inherits="Behemoth.SharePoint.Administration.WebConfigModificationAdmin" MasterPageFile="admin.master" %>

<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>

<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Collections.ObjectModel" %>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Register TagPrefix="wssuc" TagName="LinksTable" src="/_controltemplates/LinksTable.ascx" %> 
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="/_controltemplates/InputFormSection.ascx" %> <%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="/_controltemplates/InputFormControl.ascx" %> <%@ Register TagPrefix="wssuc" TagName="LinkSection" src="/_controltemplates/LinkSection.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="/_controltemplates/ButtonSection.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ActionBar" src="/_controltemplates/ActionBar.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ToolBar" src="/_controltemplates/ToolBar.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ToolBarButton" src="/_controltemplates/ToolBarButton.ascx" %> <%@ Register TagPrefix="wssuc" TagName="Welcome" src="/_controltemplates/Welcome.ascx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    Manage web.config modifications
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderPageImage" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PlaceHolderMain" runat="server">

<script type="text/javascript" language="javascript">
<!--
  function ValueChanged(textbox, item) {
    var objCheckBox;
    objCheckBox = document.getElementById('checkedItem' + item); 

    var first;
    first = document.getElementById('valueItem' + item);

    if (textbox.value != first.value) {
      objCheckBox.checked='true';
    }
    else {
      objCheckBox.checked='';
    }
  }
//-->
</script>
    <style>
        #idResults
        {
            border-collapse:collapse;
            border:1px solid black;
        }
        #idResults TD
        {
            font-family:verdana;
            font-size:8pt;
            padding:2px;
        }
        
        #idResults TH
        {
            font-family:verdana;
            font-size:10pt;
            font-weight:bold;
            text-align:left;
            padding:2px;
            border-bottom:2px solid black;
        }
        
        .header
        {
            font-weight:bold;
            padding-left:4px;
            padding-right:4px;
        }

        #ctl00_PlaceHolderMain_selector .ms-authoringcontrols 
        {
            background-color: transparent !important;
        }
 
        .w100percent 
        {
            width:100%;
        }

        .active-modification 
        {       
            font-weight:bold;
            color:black;
        }
        .active-modification TD {
            color:black;
        }
        .inactive-modification
        {
            font-style:italic;
            color:gray;
        }
        .inactive-modification TD 
        {
            font-style:italic;
            color:gray;
        }

    </style>

    <table width="100%">
        <tr><td align="right">
            <SharePoint:WebApplicationSelector id="selector" runat="server"
                OnContextChange="OnContextChange"
                AllowAdministrationWebApplication="false" />
        </td></tr>
    </table>
    <%
    if (selector.CurrentItem != null && selector.CurrentItem.WebConfigModifications.Count > 0)
    {
     %>
    <table width="100%">
        <tr>
            <td class="ms-linksectionheader" style="padding:4px"><h3 class="ms-standardheader">Current modifications</h3></td>
        </tr>
   </table>

    <table border="0" cellpadding="0" width="100%" id="idResults1" class="ms-listviewtable" style="border-collapse:collapse">
        <tr class="ms-viewheadertr">
            <th class="ms-vh2-nofilter ms-vh2-gridview" style="width:20px">&nbsp;</th>
            <th class="ms-vh2-nofilter ms-vh2-gridview">Type</th>
            <th class="ms-vh2-nofilter ms-vh2-gridview">Owner</th>
            <th class="ms-vh2-nofilter ms-vh2-gridview">Path</th>
            <th class="ms-vh2-nofilter ms-vh2-gridview">Name</th>
            <th class="ms-vh2-nofilter ms-vh2-gridview">Value</th>
            <th class="ms-vh2-nofilter ms-vh2-gridview">Sequence</th>
        </tr>
        <%
            Hashtable ActiveWebConfigModifications = new Hashtable();
            foreach (SPWebConfigModification m in selector.CurrentItem.WebConfigModifications)
            {              
              string sKey = String.Format("{0}_{1}", m.Path, m.Name);
              object modExists = ActiveWebConfigModifications[sKey];

              if(modExists==null) {
                  ActiveWebConfigModifications.Add(sKey, m);
              } 
              else {
                if (m.Sequence >= ((SPWebConfigModification) modExists).Sequence)  {
                  ActiveWebConfigModifications.Remove(sKey);
                  ActiveWebConfigModifications.Add(sKey, m);
                }
              }
            }

            int c = 0;
            foreach (SPWebConfigModification m in selector.CurrentItem.WebConfigModifications)
            {
                string ActiveModification = "inactive-modification";
                SPWebConfigModification mh = (SPWebConfigModification) ActiveWebConfigModifications[String.Format("{0}_{1}", m.Path, m.Name)];
                if (m.Equals(mh) && m.Sequence == mh.Sequence) {
                    ActiveModification = "active-modification";
                }

                %><tr class="<%=Alternate(c)%> <%=ActiveModification%>">
                    <td class="ms-vb2"><input type="checkbox" ID="checkedItem<%=c.ToString()%>" name="checkedItem<%=c.ToString()%>" /> </td>
                    <td class="ms-vb2"><%=m.Type%></td>
                    <td class="ms-vb2"><%=m.Owner%></td>
                    <td class="ms-vb2"><%=m.Path%></td>
                    <td class="ms-vb2"><%=m.Name%></td>
                    <td class="ms-vb2"><input type="text" ID="updateItem<%=c.ToString()%>" name="updateItem<%=c.ToString()%>" value="<%=m.Value%>" OnMouseUp="ValueChanged(this, <%=c.ToString()%>);" OnKeyUp="ValueChanged(this, <%=c.ToString()%>);" class="ms-input w100percent" /></td>
                    <td class="ms-vb2"><%=m.Sequence%></td>
                    <input type="hidden" value="<%=m.Value%>" ID="valueItem<%=c.ToString()%>" name="valueItem<%=c.ToString()%>" />
                </tr><% c++;                
            }
         %>
    </table>
    <br />    
    <asp:Button ID="Button1" Text="Update" runat="server" OnClick="UpdateSelected" CssClass="ms-ButtonHeightWidth"/>
    &nbsp;
    <asp:Button ID="Button2" Text="Delete" runat="server" OnClick="DeleteSelected" CssClass="ms-ButtonHeightWidth"/>
    &nbsp;    
    <span class="active-modification">Active modifications</span>
    &nbsp;|&nbsp;
    <span class="inactive-modification">Inactive modifications</span>
    <br /><br />
    <%
    }
     %>

    <%
    if (selector.CurrentItem != null)
    {
     %>

   <table width=100% cellpadding=0 cellspacing=0 border=0>
	<wssuc:LinkSection
		Title="Add new modification"
		runat="server">
		<Template_Description>
		    <asp:Label ID="idAddError" runat="server" ForeColor="red" Font-Bold="true" Font-Size="Medium" />
		</Template_Description>
		 <Template_Links>
		</Template_Links>
		<Template_OtherControls>
            <table border="0" width="100%" cellspacing="0" cellpadding="0" class="ms-descriptiontext">
                <wssuc:InputFormSection runat="server" Title="Modification Type">
                    <template_description>
                        This is the type of the modification. Element or Attribute.
                    </template_description>
                    <template_inputformcontrols>
	                    <wssuc:InputFormControl LabelText="Select the type of modification" runat="server">
		                    <Template_Control>
                                <asp:DropDownList runat="server" ID="idType">
                                    <asp:ListItem Text="EnsureAttribute" Value="1"/>
                                    <asp:ListItem Text="EnsureChildNode" Value="0"/>
                                    <asp:ListItem Text="EnsureSection" Value="2"/>
                                </asp:DropDownList>
		                    </Template_Control>
	                    </wssuc:InputFormControl>
                    </template_inputformcontrols>
                </wssuc:InputFormSection>
                <wssuc:InputFormSection runat="server" Title="Modification Owner">
                    <template_description>
                        This is the owner of the modification. The name does not have to be unique, but is it best
                        not to use a name used by another feature.
                    </template_description>
                    <template_inputformcontrols>
	                    <wssuc:InputFormControl LabelText="Enter the owner" runat="server">
		                    <Template_Control>
			                    <asp:TextBox runat=server ID=idOwner CssClass="ms-long" />
		                    </Template_Control>
	                    </wssuc:InputFormControl>
                    </template_inputformcontrols>
                </wssuc:InputFormSection>
                <wssuc:InputFormSection runat="server" Title="Modification Path">
                    <template_description>
                        This is the path to the containing XML node.
                    </template_description>
                    <template_inputformcontrols>
	                    <wssuc:InputFormControl LabelText="Enter the path" runat="server">
		                    <Template_Control>
			                    <asp:TextBox runat=server ID=idPath CssClass="ms-long" />
		                    </Template_Control>
	                    </wssuc:InputFormControl>
                    </template_inputformcontrols>
                </wssuc:InputFormSection>
                <wssuc:InputFormSection runat="server" Title="Modification Name">
                    <template_description>
                        This is the name of the modification. If you are modifying an attribute this will be the name of
                        the attribute. If you are modifying or adding an XML Element this wil be the XPath to uniquely
                        identify the element within it's parent node.
                    </template_description>
                    <template_inputformcontrols>
	                    <wssuc:InputFormControl LabelText="Enter the name" runat="server">
		                    <Template_Control>
			                    <asp:TextBox runat=server ID=idName CssClass="ms-long" />
		                    </Template_Control>
	                    </wssuc:InputFormControl>
                    </template_inputformcontrols>
                </wssuc:InputFormSection>
                <wssuc:InputFormSection runat="server" Title="Modification Value">
                    <template_description>
                        This is the value of the modification. If you are adding or modifying an attribute this will be the
                        value of the attribute. If you are adding or modifying an Element this will be the entire element as you
                        wish it to be added.
                    </template_description>
                    <template_inputformcontrols>
	                    <wssuc:InputFormControl LabelText="Enter the value" runat="server">
		                    <Template_Control>
			                    <asp:TextBox runat=server ID=idValue CssClass="ms-long" />
		                    </Template_Control>
	                    </wssuc:InputFormControl>
                    </template_inputformcontrols>
                </wssuc:InputFormSection>
                <wssuc:InputFormSection runat="server" Title="Modification Sequence">
                    <template_description>
                        This is the sequence of the modification. 
                    </template_description>
                    <template_inputformcontrols>
	                    <wssuc:InputFormControl LabelText="Enter the sequence" runat="server">
		                    <Template_Control>
			                    <asp:TextBox runat=server ID=idSequence Text="0"  CssClass="ms-input"/>
		                    </Template_Control>
	                    </wssuc:InputFormControl>
                    </template_inputformcontrols>
                </wssuc:InputFormSection>
                <wssuc:ButtonSection runat="server">
                    <template_buttons>
		                <asp:Button ID="Button3" UseSubmitBehavior="false" runat="server" class="ms-ButtonHeightWidth" OnClick="AddEntry" Text="Add New" accesskey="A"/>
	                </template_buttons>
                </wssuc:ButtonSection>
            </table>
		</Template_OtherControls>
	</wssuc:LinkSection>
   </table>

   <table width=100% cellpadding=0 cellspacing=0 border=0>
	<wssuc:LinkSection
		Title="Samples"
		runat="server">
		<Template_Description>
		    Here are some sample entries to demonstrate how the entries are built.
		</Template_Description>
		 <Template_Links>
		</Template_Links>
		<Template_OtherControls>
            <table border="0" cellpadding="0" width="100%" id="Table1" class="ms-listviewtable" style="border-collapse:collapse">
                <tr class="ms-viewheadertr">
                    <th class="ms-vh2-nofilter ms-vh2-gridview">Type</th>
                    <th class="ms-vh2-nofilter ms-vh2-gridview">Path</th>
                    <th class="ms-vh2-nofilter ms-vh2-gridview">Name</th>
                    <th class="ms-vh2-nofilter ms-vh2-gridview">Value</th>
                    <th class="ms-vh2-nofilter ms-vh2-gridview">Description</th>
<!--
                    <th class="ms-vh2-nofilter ms-vh2-gridview" style="width:75px">&nbsp;</th>
-->
                </tr>
                <tr>
                    <td class="ms-vb2">EnsureAttribute</td>
                    <td class="ms-vb2">configuration/SharePoint/SafeMode</td>
                    <td class="ms-vb2">CallStack</td>
                    <td class="ms-vb2">true</td>
                    <td class="ms-vb2" style="font-weight:bold">Enable SafeMode CallStack</td>
<!--
                    <td class="ms-vb2"><input type="button" value="Copy" class="ms-ButtonHeightWidth"/></td>
-->
                </tr>
                <tr>
                    <td class="ms-vb2">EnsureChildNode</td>
                    <td class="ms-vb2">configuration/appSettings</td>
                    <td class="ms-vb2">add[@key="newKey"]</td>
                    <td class="ms-vb2">&lt;add key="newKey" value="new Value" /&gt;</td>
                    <td class="ms-vb2" style="font-weight:bold">Add new appSetting</td>
<!--
                    <td class="ms-vb2"><input type="button" value="Copy" class="ms-ButtonHeightWidth"/></td>
-->
                </tr>
                <tr>
                    <td class="ms-vb2">EnsureAttribute</td>
                    <td class="ms-vb2">configuration/system.web/customErrors</td>
                    <td class="ms-vb2">mode</td>
                    <td class="ms-vb2">Off</td>
                    <td class="ms-vb2" style="font-weight:bold">Custom errors off</td>
<!--
                    <td class="ms-vb2"><input type="button" value="Copy" class="ms-ButtonHeightWidth"/></td>
-->
                </tr>
                <tr>
                    <td class="ms-vb2">EnsureAttribute</td>
                    <td class="ms-vb2">configuration/system.web/trust</td>
                    <td class="ms-vb2">level</td>
                    <td class="ms-vb2">Full</td>
                    <td class="ms-vb2" style="font-weight:bold">Enable Full trust</td>
<!--
                    <td class="ms-vb2"><input type="button" value="Copy" class="ms-ButtonHeightWidth"/></td>
-->
                </tr>
             </table>
		</Template_OtherControls>
	</wssuc:LinkSection>
    </table>
<% } %>

    <%
    if (selector.CurrentItem == null)
    {
     %>
        <h2>Please select a web application to modify</h2>
<% } %>
</asp:Content>
